package com.java.tutorials.spring.aop;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;
/**
 * Created by xuzhike on 2016/1/30.
 * http://www.java2s.com/Code/Java/Spring/ImplementsMethodBeforeAdviceToCheckParameters.htm
 * Implements MethodBeforeAdvice To Check Parameters
 */
public class MethodBeforeAdviceParam extends SpringAOPTester {
    public void test(){
        MtBean testBean = (MtBean) ctx
                .getBean("afterBean");
        testBean.showValues();
    }
}

class MtBean {
    private String firstName;

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void showValues() {
        System.out.println("First name: " + this.firstName);
    }
}
class NullArgumentsNotAllowedBeforeAdvice implements MethodBeforeAdvice {
    public void before(Method method, Object[] arguments, Object target)
            throws Throwable {
        if (arguments == null || arguments.length == 0) {
            return;
        }

        for (int i = 0; i < arguments.length; i++) {
            Object argument = arguments[i];
            if (argument == null) {
                throw new IllegalArgumentException(
                        "Value for argument [" + i + "] is required but not present " +
                                "for method [" + method + "]!"
                );
            }
        }
    }
}


